import Vue from "vue"
import Vuex from "vuex"
import router from "@/router"

Vue.use(Vuex)

export default new Vuex.Store({
	state: {
		routes: [],
		setting: {
			theme: localStorage.getItem("theme") ?? "dark",
		},
	},
	getters: {
		getRoutes(state) {
			const routes = router.options.routes.filter(
				(item) => item.path === "/"
			)[0].children
			state.routes = routes
			return state.routes
		},
		getSetting(state) {
			return state.setting
		},
	},
	mutations: {
		set_routes(state, routes) {
			state.routes = routes
		},
		set_setting(state, setting) {
			state.setting = setting
		},
	},
	actions: {
		setSetting({ commit }, data) {
			const { theme } = data
			return new Promise((resolve) => {
				localStorage.setItem("theme", theme)
				commit("set_setting", data)
				resolve()
			})
		},
	},
	modules: {},
})
